<HTML>
<HEAD>
<TITLE>[Chapter 7] 7.2 FlowLayout</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:39:17 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch07_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 7<br>Layouts</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch07_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-7-SECT-2">7.2 FlowLayout</A></h2>

<P CLASS=para>
<A NAME="CH07.FLOW1"></A><A NAME="CH07.FLOW2"></A><A NAME="CH07.FLOW3"></A><tt CLASS=literal>FlowLayout</tt> is the default <tt CLASS=literal>LayoutManager</tt> 
for a <tt CLASS=literal>Panel</tt>. A <tt CLASS=literal>FlowLayout</tt> 
adds components to the container in rows, working from left to right. When 
it can't fit any more components in a row, it starts a new row--not 
unlike a word processor with word wrap enabled. When the container gets 
resized, the components within it get repositioned based on the container's 
new size. If sufficient space is available, components within <tt CLASS=literal>FlowLayout</tt> 
containers are given their preferred size. If there is insufficient space, 
you do not see the components in their entirety. 

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-7-SECT-2.1">FlowLayout Methods</A></h3>Constants

<P CLASS=para>
<tt CLASS=literal>FlowLayout</tt> defines three constants, 
all of which are used to specify alignment. The alignment tells <tt CLASS=literal>FlowLayout</tt> 
where to start positioning the components 
on each row. Each component is still added from left to right, no matter 
what the alignment setting is. 

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public final static int LEFT </I><br>
<DD>

<P CLASS=para>
<tt CLASS=literal>LEFT</tt> is the constant for left 
alignment. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public final static int CENTER </I><br>
<DD>

<P CLASS=para>
<tt CLASS=literal>CENTER</tt> is the constant for 
center alignment and is the default. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public final static int RIGHT </I><br>
<DD>

<P CLASS=para>
<tt CLASS=literal>RIGHT</tt> is the constant for right 
alignment. </DL>
Constructors

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public FlowLayout () </I><br>
<DD>

<P CLASS=para>
This constructor creates a <tt CLASS=literal>FlowLayout</tt> 
using default settings: center alignment with a horizontal and vertical 
gap of five pixels. The gap is the space between the different components 
in the different directions. By default, there will be five pixels between 
components. The constructor is usually called within a call to <tt CLASS=literal>setLayout()</tt>: 
<tt CLASS=literal>setLayout (new FlowLayout())</tt>. 
<A HREF="ch07_02.htm#JAWT-CH-7-FIG-1">Figure 7.1</A> shows how the default <tt CLASS=literal>FlowLayout</tt> 
behaves with different screen sizes. As the screen C shows, if the screen 
is too small, the components will <I CLASS=emphasis>not </I>be 
shrunk so that they can fit better. </DL>
<DIV CLASS=figure>
<h4 CLASS=figure><A CLASS="TITLE" NAME="JAWT-CH-7-FIG-1">Figure 7.1: FlowLayout with six buttons and three different screen sizes</A></h4>


<p>
<img align=middle src="./figs/jawt0701.gif" alt="[Graphic: Figure 7-1]" width=503 height=313 border=0>

</DIV>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public FlowLayout (int alignment) </I><br>
<DD>

<P CLASS=para>
This version of the constructor creates a <tt CLASS=literal>FlowLayout</tt> 
using the specified <tt CLASS=literal>alignment</tt> 
and a horizontal and vertical gap of five pixels. Valid alignments are 
the <tt CLASS=literal>FlowLayout</tt> constants, although 
there is no verification. <A HREF="ch07_02.htm#JAWT-CH-7-FIG-2">Figure 7.2</A> shows the effect 
of different alignments: <tt CLASS=literal>FlowLayout.LEFT</tt> (screen A), 
<tt CLASS=literal>FlowLayout.CENTER</tt> (B), and <tt CLASS=literal>FlowLayout.RIGHT</tt> (C). </DL>
<DIV CLASS=figure>
<h4 CLASS=figure><A CLASS="TITLE" NAME="JAWT-CH-7-FIG-2">Figure 7.2: FlowLayout with three different alignments</A></h4>


<p>
<img align=middle src="./figs/jawt0702.gif" alt="[Graphic: Figure 7-2]" width=503 height=173 border=0>

</DIV>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public FlowLayout (int alignment, int hgap, int vgap) </I><br>
<DD>

<P CLASS=para>
The final version of the constructor is called by the other two. It requires 
you to explicitly specify the alignment, horizontal gap (<tt CLASS=literal>hgap</tt>), 
and vertical gap (<tt CLASS=literal>vgap</tt>). 
This creates a <tt CLASS=literal>FlowLayout</tt> with 
an alignment of <tt CLASS=literal>alignment</tt>, 
horizontal gap of <tt CLASS=literal>hgap</tt>, and 
vertical gap of <tt CLASS=literal>vgap</tt>. The units 
for gaps are pixels. It is possible to have negative gaps if you want components 
to be placed on top of one another. <A HREF="ch07_02.htm#JAWT-CH-7-FIG-3">Figure 7.3</A>
 shows the effect of changing the gap sizes.</DL>
<DIV CLASS=figure>
<h4 CLASS=figure><A CLASS="TITLE" NAME="JAWT-CH-7-FIG-3">Figure 7.3: FlowLayout with hgap of 0 and vgap of 20</A></h4>


<p>
<img align=middle src="./figs/jawt0703.gif" alt="[Graphic: Figure 7-3]" width=184 height=206 border=0>

</DIV>

Informational methods

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public int getAlignment () <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getAlignment()</tt> method retrieves 
the current alignment of the <tt CLASS=literal>FlowLayout</tt>. 
The return value should equal one of the class constants <tt CLASS=literal>LEFT</tt>, 
<tt CLASS=literal>CENTER</tt>, or <tt CLASS=literal>RIGHT</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void setAlignment (int alignment) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>setAlignment()</tt> method changes 
the <tt CLASS=literal>FlowLayout</tt> alignment to 
<tt CLASS=literal>alignment</tt>. The alignment value 
should equal one of the class constants <tt CLASS=literal>LEFT</tt>, 
<tt CLASS=literal>CENTER</tt>, or <tt CLASS=literal>RIGHT</tt>, 
but this method does not check. After changing the alignment, you must 
<tt CLASS=literal>validate()</tt> the <tt CLASS=literal>Container</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public int getHgap () <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getHgap()</tt> method retrieves 
the current horizontal gap setting. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void setHgap (int hgap) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>setHgap()</tt> method changes 
the current horizontal gap setting to <tt CLASS=literal>hgap</tt>. 
After changing the gaps, you must <tt CLASS=literal>validate()</tt> 
the <tt CLASS=literal>Container</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public int getVgap () <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getVgap()</tt> method retrieves 
the current vertical gap setting. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void setVgap (int hgap) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>setVgap()</tt> method changes 
the current vertical gap setting to <tt CLASS=literal>vgap</tt>. 
After changing the gaps, you must <tt CLASS=literal>validate()</tt> 
the <tt CLASS=literal>Container</tt>. </DL>
LayoutManager methods

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public void addLayoutComponent (String name, Component component)  </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>addLayoutComponent()</tt> method 
of <tt CLASS=literal>FlowLayout</tt> does nothing. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void removeLayoutComponent (Component component)  </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>removeLayoutComponent()</tt> 
method of <tt CLASS=literal>FlowLayout</tt> does nothing. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Dimension preferredLayoutSize (Container target)  </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>preferredLayoutSize()</tt> method 
of <tt CLASS=literal>FlowLayout</tt> calculates the 
preferred dimensions for the <tt CLASS=literal>target</tt> 
container. The <tt CLASS=literal>FlowLayout</tt> computes 
the preferred size by placing all the components in one row and adding 
their individual preferred sizes along with gaps and insets. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Dimension minimumLayoutSize (Container target)  </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>minimumLayoutSize()</tt> method 
of <tt CLASS=literal>FlowLayout</tt> calculates the 
minimum dimensions for the container by adding up the sizes of the components. 
The <tt CLASS=literal>FlowLayout</tt> computes the 
minimum size by placing all the components in one row and adding their 
individual minimum sizes along with gaps and insets. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void layoutContainer (Container target)  </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>layoutContainer()</tt> method 
draws <tt CLASS=literal>target</tt>'s components 
on the screen, starting with the first row of the display, going left to 
right across the screen, based on the current alignment setting. When it 
reaches the right margin of the container, it skips down to the next row, 
and continues drawing additional components. </DL>
Miscellaneous methods

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public String toString () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>toString()</tt> method of <tt CLASS=literal>FlowLayout</tt> 
returns the current horizontal and vertical gap settings along with the 
alignment (left, center, right). For a <tt CLASS=literal>FlowLayout</tt> 
that uses all the defaults, <tt CLASS=literal>toString()</tt> 
produces: </DL>
<DIV CLASS=screen>
<P>
<PRE>
java.awt.FlowLayout[hgap=5,vgap=5,align=center]
</PRE>
</DIV>

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch07_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch07_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>The LayoutManager Interface</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>BorderLayout</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
